package com.wanmait.zaisousuo.service.Impl;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import com.wanmait.zaisousuo.service.CityService;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
import com.wanmait.zaisousuo.pojo.City;
import com.wanmait.zaisousuo.mapper.CityMapper;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class CityServiceImpl implements CityService {

    @Resource
    private CityMapper cityMapper;
    @Resource
    private RedisTemplate<String,Object> redisTemplate;

    @Override
    public void insert(City object) {

    }

    @Override
    public void update(City object) {

    }

    @Override
    public void delete(Integer id) {

    }

    @Override
    public void delete(String ids) {

    }

    @Override
    public City findById(Integer id) {
        return null;
    }

    @Override
    public List<City> findAll() {
        return null;
    }

    @Override
    public List<City> findAll(City object) {
        return null;
    }

    @Override
    public int getDataCount() {
        return 0;
    }

    @Override
    public int getDataCount(City object) {
        return 0;
    }

    @Override
    public PageInfo<City> findList(int pageNum) {
        return null;
    }

//  省份列表用字符串表存储 存储方式 province List<Provice>对象
    @Override
    public List<City> findProvince() {
        String provincesStr = (String)redisTemplate.opsForValue().get("provinces");
        List<City> provinces = null;
        if (provincesStr==null){
            provinces = cityMapper.findProvince();
            provincesStr = JSON.toJSONString(provinces);
            redisTemplate.opsForValue().set("provinces",provincesStr);
        }
        else {
            provinces = JSON.parseArray(provincesStr,City.class);
        }
        return provinces;
    }

    @Override
    public List<City> findCity(Integer id) {
        String citiesStr = (String)redisTemplate.opsForHash().get("cities",id+"");
        List<City> cities = null;
        if(citiesStr==null){
            cities = cityMapper.findCity(id);
            citiesStr = JSON.toJSONString(cities);
            redisTemplate.opsForHash().put("cities",id+"",citiesStr);

        }
        else {
            cities = JSON.parseArray(citiesStr,City.class);
        }
        return cities;
    }

    @Override
    public City findByName(String keyword,Boolean flag) {
        return cityMapper.findByName(keyword,flag);
    }
}
